package com.longbo.leaf.portal.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.longbo.leaf.common.BaseException;
import com.longbo.leaf.common.Status;
import com.longbo.leaf.config.IdConfig;
import com.longbo.leaf.dto.UserRegisterDTO;
import com.longbo.leaf.portal.mapper.UserRoleMapper;
import com.longbo.leaf.portal.model.User;
import com.longbo.leaf.portal.mapper.UserMapper;
import com.longbo.leaf.portal.model.UserRole;
import com.longbo.leaf.portal.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author longbo
 * @since 2021-02-01
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    Snowflake snowflake;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    @Transactional
    public void userRegister(UserRegisterDTO userRegisterDTO) {

        //判断用户名是否已经被注册
       Optional<User> result = userMapper.findUserByUsername(userRegisterDTO.getUsername());
       if (result.isPresent()){
           throw new BaseException(Status.USER_HAS_EXIST);
       }

       //当前时间
        LocalDateTime now = LocalDateTime.now();

        //插入数据到User表
        User user = new User();
        long userId = snowflake.nextId();
        user.setId(userId);
        user.setUsername(userRegisterDTO.getUsername());
        String rawPassword = userRegisterDTO.getPassword();
        String encodePassword = passwordEncoder.encode(rawPassword);
        user.setPassword(encodePassword);
        user.setNickname(userRegisterDTO.getNickname());
        user.setRealName(userRegisterDTO.getRealName());
        user.setAddress(userRegisterDTO.getAddress());
        user.setHobby(userRegisterDTO.getHobby());
        user.setPhone(userRegisterDTO.getUsername());
        user.setGender(userRegisterDTO.getGender());
        user.setStatus(1);
        user.setEmail(userRegisterDTO.getEmail());
        user.setGmtCreated(now);
        user.setGmtModified(now);
        int rows = userMapper.insert(user);
        if (rows!=1){
            throw new BaseException(Status.ERROR);
        }

        //插入数据到user_role表
        UserRole  userRole = new UserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(1356166256268546050L);
        userRole.setGmtCreated(now);
        userRole.setGmtModified(now);
        rows = userRoleMapper.insert(userRole);
        if (rows!=1){
            throw new BaseException(Status.ERROR);
        }
    }

    @Override
    public List<User> selectUserAll() {
        return userMapper.selectUserAll();
    }

    @Override
    @Transactional
    public void deleteUserByUserId(String userId) {
        int rows = userMapper.deleteUserByUserId(userId);
        if (rows!=1){
            throw new BaseException(Status.ERROR);
        }

        rows = userRoleMapper.deleteByUserId(userId);
        if (rows!=1){
            throw new BaseException(Status.ERROR);
        }
    }

    @Override
    public int updateByUserIdAndRoleIdInt(Long userId, Long roleId) {
      int rows =  userRoleMapper.updateByUserIdAndRoleIdInt(userId,roleId);
      if (rows!=1){
          throw new BaseException(Status.ERROR);
      }
      return rows;
    }
}
